<?php
# 导入数据库接口文件
require_once 'dr-app-config.php';
require_once 'dr-app-functions.php';
require_once 'dr-db-options.php';

# 获得用户的请求
$action = isset($_POST['action']) ? $_POST['action'] : '';

# 根据用户的请求进行不同的响应
switch ($action) {
	case "get_all_tags":
		echo dr_get_tag_all();
		break;

	case "get_tag_detail":
		echo dr_get_tag_detail();
		break;

	case "mark_tag":
		echo dr_mark_tag();
		break;
	case "mark_tag":
		echo dr_mark_tag();
		break;

	default:
		echo '';
}

function dr_get_tag_all()
{
	global $db;

	$tags = array();

	$order = $_POST['order'];
	$user_id = $_POST['user_id'];
	$match_str = $_POST['match_str'];
	$rank = $_POST['rank'];

	$result = '';
	$top_tag_num = dr_db_get_option('tag_top_num');

	if ($order == 'number') {
		if (empty($match_str))
			$result = dr_db_query_sub($db, 'select * from ^tags order by question_num desc');
		else {
			$match_str .= '%';
			$result = dr_db_query_sub($db, 'select * from ^tags where title like $ order by question_num desc', $match_str);
		}
	} elseif ($order == 'name') {
		if (empty($match_str))
			$result = dr_db_query_sub($db, 'select * from ^tags order by convert(title using gbk)');
		else {
			$match_str .= '%';
			$result = dr_db_query_sub($db, 'select * from ^tags where title like $ order by convert(title using gbk)', $match_str);
		}
	} elseif (!empty($rank)) {
		dr_get_rank_tag($result);
	} else {
		if (empty($match_str))
			$result = dr_db_query_sub($db, 'select * from ^tags order by question_num desc');
		else {
			$match_str .= '%';
			$result = dr_db_query_sub($db, 'select * from ^tags where title like $ order by question_num desc', $match_str);
		}
	}
	while ($row = mysql_fetch_assoc($result)) {
		$row['is_marked'] = false;
		if ($user_id) {
			$result_mark = dr_db_query_sub($db, 'select * from ^mark_tag where user_id=# and marked_tag_id=#', $user_id, $row['tag_id']);
			if (mysql_num_rows($result_mark) == 0)
				$row['is_marked'] = false;
			else
				$row['is_marked'] = true;
		}
		if (!empty($rank)) {
			$interval = 0;
			switch ($rank)
			{
				case 'd4':
					$interval = 3600 * 24 * 1;
					break;
				case 'w4':
					$interval = 3600 * 24 * 7;
					break;
				case 'm4':
					$interval = 3600 * 24 * 30;
					break;
			}
			$row['add_mark_num'] = dr_db_read_one_value(dr_db_query_sub($db, 'select count(marked_tag_id) from dr_mark_tag where' .
			                                                                 ' marked_tag_id=# and unix_timestamp(date_add(now(),interval 8 hour))-unix_timestamp(mark_date)<# group by marked_tag_id', $row['tag_id'], $interval));
			if (empty($row['add_mark_num']))
				$row['add_mark_num'] = 0;
		}
		array_push($tags, $row);
	}
	/******************************************************/

	$setting['show_day_top'] = dr_db_get_option('display_day_top');
	$setting['show_week_top'] = dr_db_get_option('display_week_top');
	$setting['show_month_top'] = dr_db_get_option('display_month_top');
	$setting['show_all_top'] = dr_db_get_option('display_all_top');


	$result_array = array();
	$result_array['tags'] = $tags;
	$result_array['topNum'] = $top_tag_num;
	$result_array['setting'] = $setting;
	echo json_encode($result_array);
}


function dr_get_tag_detail() {
	global $db;
	$id = $_POST['id'];

	$result = dr_db_read_one_assoc(dr_db_query_sub($db, "SELECT * FROM ^tags WHERE `tag_id` = $id LIMIT 1"));

	return json_encode($result);
}

function dr_get_rank_tag(&$result)
{
	global $db;
	$rank = $_POST['rank'];
	$interval = 0;
	$start = 0;
	$rank_num = 5;
	if (preg_match("/[dwm]4/", $rank)) {
		switch ($rank)
		{
			case 'd4':
				$interval = 3600 * 24 * 1;
				break;
			case 'w4':
				$interval = 3600 * 24 * 7;
				break;
			case 'm4':
				$interval = 3600 * 24 * 30;
				break;
		}
		$result = dr_db_read_all_values(dr_db_query_sub($db, 'select question_id ' .
		                                                     'from ^questions where unix_timestamp(date_add(now(),interval 8 hour))-unix_timestamp(updated_date)<#', $interval));
		$lists = '(' . implode(',', $result) . ')';
		$result = dr_db_query_sub($db, 'select T.*,count(Q.tag_id) as add_num from ^question_tag Q,^tags T ' .
		                               'where Q.tag_id=T.tag_id and Q.question_id in # group by Q.tag_id order by add_num desc limit #,#', $lists, $start, $rank_num);
	}
	else if ($rank == 'a4') {
		$result = dr_db_query_sub($db, 'select * from ^tags order by question_num desc limit #,#', $start, $rank_num);
	}
}

function dr_mark_tag()
{
	global $db;

	$user_id = $_POST['user_id'];
	$marked_tag_id = $_POST['marked_tag_id'];
	$mark_type = $_POST['mark_type'];

	if ($mark_type == 'mark') {
		$result = dr_db_query_sub($db, 'insert into ^mark_tag values(#,#,date_add(now(),interval 8 hour))', $user_id, $marked_tag_id);
		if (!$result) {
			return '{"ret":0}';
		}

		//将此行为记录到用户的历史记录表中
		$result = dr_db_query_sub($db, 'insert into ^action_history(user_id,action_type,action_date,object_id)' .
		                               ' values(#,\'MT\',date_add(NOW(),interval 8 hour),#)', $user_id, $marked_tag_id);
		if (!$result) {
			return '{"ret":0}';
		}
	}
	else if ($mark_type == 'cancel_mark') {
		$result = dr_db_query_sub($db, 'delete from ^mark_tag where user_id=# and marked_tag_id=#', $user_id, $marked_tag_id);
		if (!$result) {
			return '{"ret":0}';
		}

		//将此行为记录到用户的历史记录表中
		$result = dr_db_query_sub($db, 'insert into ^action_history(user_id,action_type,action_date,object_id)' .
		                               ' values(#,\'UMT\',date_add(NOW(),interval 8 hour),#)', $user_id, $marked_tag_id);
		if (!$result) {
			return '{"ret":0}';
		}
	}
	return '{"ret":1}';
}

